package cn.tedu.medicalappointmentsystemk.controller;

import cn.tedu.medicalappointmentsystemk.consts.web.HttpConsts;
import cn.tedu.medicalappointmentsystemk.pojo.authentication.CurrentPrincipal;
import cn.tedu.medicalappointmentsystemk.pojo.bo.UserBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.UserSelectByIdBO;
import cn.tedu.medicalappointmentsystemk.pojo.param.*;
import cn.tedu.medicalappointmentsystemk.pojo.bo.UserSelectBO;
import cn.tedu.medicalappointmentsystemk.response.JsonResult;
import cn.tedu.medicalappointmentsystemk.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("v1/users")
@Slf4j
@Api(tags = "用户模块")
public class UserController implements HttpConsts {
    @Autowired
    private UserService userService;

    @PostMapping("reg")
    @ApiOperationSupport(order = 610)
    @ApiOperation("注册")
    public JsonResult Reg(UserRegParam userRegParam){
        log.info("开始执行注册功能,入参:{}",userRegParam);
        userService.reg(userRegParam);
        log.info("执行注册功能结束");
        return JsonResult.ok();
    }

    @PostMapping("login")
    @ApiOperationSupport(order = 620)
    @ApiOperation("登录")
    public JsonResult Login(@Validated UserLoginParam userLoginParam,
                            @ApiIgnore HttpServletRequest request){
        log.info("开始执行登录功能,入参:{}",userLoginParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HEADER_USER_AGENT);
        UserBO userBO = userService.login(userLoginParam,remoteAddr,userAgent);
        log.info("执行登录功能完成,入参:{},出参:{}",userLoginParam,userBO);
        return JsonResult.ok(userBO);
    }

    @PostMapping("logout")
    @ApiOperationSupport(order = 630)
    @ApiOperation("退出登录")
    public JsonResult Logout(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        log.info("开始执行退出登录功能,入参:{}",currentPrincipal);
        userService.logout(currentPrincipal);
        log.info("执行退出登录功能结束");
        return JsonResult.ok();
    }

    @PostMapping("changePassword")
    @PreAuthorize("hasAuthority('/users/update')")
    @ApiOperationSupport(order = 640)
    @ApiOperation("修改密码")
    public JsonResult ChangePassword(UserChangePasswordParam userChangePasswordParam){
        log.info("开始执行修改密码功能,入参:{}",userChangePasswordParam);
        userService.changePassword(userChangePasswordParam);
        log.info("执行修改密码功能结束");
        return JsonResult.ok();
    }

    @PostMapping("forgetPassword")
    @PreAuthorize("hasAuthority('/users/update')")
    @ApiOperationSupport(order = 650)
    @ApiOperation("忘记密码")
    public JsonResult ForgetPassword(UserForgetPasswordParam userForgetPasswordParam){
        log.info("开始执行忘记密码功能,入参:{}",userForgetPasswordParam);
        userService.changePassword(userForgetPasswordParam);
        log.info("执行忘记密码功能结束");
        return JsonResult.ok();
    }

    @GetMapping("selectUser/{id}")
    @PreAuthorize("hasAuthority('/users/select')")
    @ApiOperationSupport(order = 660)
    @ApiOperation("查询用户信息")
    public JsonResult SelectUserById(@PathVariable Long id){
        log.info("开始执行通过id:[{}]查询用户信息功能",id);
        UserSelectByIdBO userSelectByIdBO = userService.selectUserById(id);
        log.info("执行通过id:[{}]查询用户信息结束,出参:{}",id,userSelectByIdBO);
        return JsonResult.ok(userSelectByIdBO);
    }

    @GetMapping("select/{id}")
    @PreAuthorize("hasAuthority('/users/select')")
    @ApiOperationSupport(order = 670)
    @ApiOperation("查询用户部分信息")
    public JsonResult SelectById(@PathVariable Long id){
        log.info("开始执行通过id:[{}]查询用户信息功能",id);
        UserSelectBO userSelectBO = userService.selectById(id);
        log.info("执行通过id:[{}]查询用户信息功能结束,出参:{}",id,userSelectBO);
        return JsonResult.ok(userSelectBO);
    }

    @PostMapping("updateUser")
    @PreAuthorize("hasAuthority('/users/update')")
    @ApiOperationSupport(order = 680)
    @ApiOperation("修改个人信息")
    public JsonResult UpdateUser(UpdateUserParam updateUserParam){
        log.info("开始执行修改个人信息功能,入参:{}",updateUserParam);
        userService.changeUser(updateUserParam);
        log.info("执行修改个人信息功能结束");
        return JsonResult.ok();
    }

    @GetMapping("select")
    @PreAuthorize("hasAuthority('/users/select')")
    @ApiOperationSupport(order = 690)
    @ApiOperation("查询全部患者信息")
    public JsonResult SelectUser(){
        log.info("开始执行查询全部患者信息功能");
        List<UserSelectBO> userSelectBOS = userService.select();
        return JsonResult.ok(userSelectBOS);
    }
}
